﻿using lxwmvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace lxwmvc
{
    class WeiXin_UserInfo : IWeiXin
    {
        /// <summary>
        /// true 记录用户的位置，并跳转到用户授权的页面
        /// </summary>
        /// <returns></returns>
        public bool CheckUserInfo(string url,string scope)
        {
            //判断用户cookies中的unionid是否存在

            //判断数据库中是否存在

            //是否关注没有太大关系

            //得到cookiesid 静默授权只能得到openid
            //var unionid = cookieshelper.getcookie("unionid").tostr();
            
#if DEBUG
            //return true;
#endif
            var openid = "";
            //如果来源是浏览器？
            
            {
                openid = cookieshelper.getcookie("openidx").tostr();
            }

            var pb = HttpContext.Current;

            //如果cookies不存在，就重新拉取这些信息
            if (openid == "")
            {
                pb.Session["login_url"] = pb.Request.Url.PathAndQuery;
                var useragent = HttpContext.Current.Request.UserAgent;

                if (defaultcontroller.TEST || - 1 == useragent.IndexOf("MicroMessenger", StringComparison.OrdinalIgnoreCase))
                {
                    HttpContext.Current.Response.Redirect("/weixin/index_pc");
                    return true;
                }
                
                

                dataloger.info(pb.Request.Url.PathAndQuery);
                //静默授权
                weixincontroller.GZH.RedirectWeiXin(url, scope);
                return true;
            }

            return false;
        }

       

        public  string UrlEncode(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }

            return (sb.ToString());
        }
    }
}
